package cn.tedu.sgos.user.controller;
import cn.tedu.sgos.base.response.JsonResult;
import cn.tedu.sgos.user.pojo.dto.UserLoginParam;
import cn.tedu.sgos.user.pojo.dto.UserSaveParam;
import cn.tedu.sgos.user.pojo.vo.UserVO;
import cn.tedu.sgos.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;



/**
 * @Author: Miyako
 * @Date: 2024-08-30-17:05
 * @Description:
 */
@RestController
@Api(tags = "用户模块")
@RequestMapping("/v1/user")
@Slf4j
public class UserController {
    @Autowired
    UserService userService;

    @ApiOperation("账号登录")
    @PostMapping("login/account")
    JsonResult login(@RequestBody UserLoginParam userLoginParam){
        log.debug(userLoginParam+"");
        UserVO userVO = userService.login(userLoginParam);
        log.debug("验证通过!");
        return JsonResult.ok(userVO) ;
    }

    /*
     * @param: []
     * @return: code:2000,data:base64位编码的序列化后的验证码图片数据
     * @description:
     **/
    @ApiOperation("获取验证码")
    @GetMapping("cap")
    public JsonResult getCaptcha(){
        log.debug("1.开始生成验证码");
        String captcha = userService.getCaptcha();
        return JsonResult.ok(captcha);
    }


    @ApiOperation("发送短信验证码")
    @PostMapping("/sendSMS/{phone}")
    public JsonResult sendSMS(@PathVariable String phone){
        log.debug("接收到手机号{}",phone);
        userService.sendSMS(phone);
        return JsonResult.ok();
    }


    @ApiOperation("手机验证码登录")
    @PostMapping("/login/SMS/{phone}/{auCode}")
    public JsonResult checkSMS(@PathVariable String phone , @PathVariable String auCode){
        log.debug("接收到数据{}{}",phone,auCode);
        UserVO userVO = userService.checkSMS(phone,auCode);
        log.debug("验证成功准备登录");
        return JsonResult.ok(userVO);
    }

    @ApiOperation("注册用户")
    @PostMapping("register")
    public JsonResult register (@RequestBody @Validated UserSaveParam register){
        log.debug("接收到数据{}",register);
        userService.saveUser(register);
        log.debug("注册成功");
        return JsonResult.ok();
    }

    @ApiOperation("找回密码/修改密码")
    @PostMapping("changePwd")
    public JsonResult changePwd (@RequestBody @Validated UserSaveParam saveParam) {
        log.debug("接收到数据{}",saveParam);
        userService.changePwd(saveParam);
        log.debug("修改成功");
        return JsonResult.ok();
    }


}
